package com.oocl.oopdemo.mycollection;

public class MyLinkedList {
//单链表
	class Node{
		int data;
		Node next;
		
		Node(int data){
			this.data=data;
		}
		
		public int getData() {
			return data;
		}
		public void setData(int data) {
			this.data = data;
		}
		public Node getNext() {
			return next;
		}
		public void setNext(Node next) {
			this.next = next;
		}	
		
	}
	
	private Node head;//初始节点
	
	public void add(int data){
		Node node=new Node(data);
		if(head==null){
			head=node;
		}
		else{
			Node cursor=head;
			while(cursor.getNext()!=null){
				cursor=cursor.getNext();
			}
			cursor.setNext(node);
		}
			
	}
	
	public void show(){
		String mll="MyLinkedList:";
			Node cursor=head;
			while(cursor!=null){
				mll +=cursor.getData()+"-->";
				cursor=cursor.getNext();		
			}
			mll +="null";
			System.out.println(mll);
		
	}
	
	public int size(){
		Node cursor=head;
		int length=0;
		while(cursor!=null){
			cursor=cursor.getNext();
			length++;
		}
		
		return length;
		
	}
	
	public boolean delete(int num){
		
		boolean flag=false;
		Node cursor=head;
		if(this.head.getNext()==null){
			return flag;
		}
		Node p=cursor.getNext();
		while(p!=null&&!(p.getData()==num)){
			cursor=p;
			p=p.getNext();
		}
		if(p!=null&&p.getData()==num){
			cursor=cursor.getNext();
			cursor=p.getNext();
					flag=true;
		}
		return flag;	
	}
	
}
